package com.zpkj.portal.dto;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zpkj.base.H5Content;
import com.zpkj.portal.utils.Constant;
import com.zpkj.portal.utils.JsonUtils;
import com.zpkj.portal.utils.des3.Des3H5;
import com.zpkj.portal.utils.des3.Des3Utils;

public class ResponseParameter implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = LoggerFactory.getLogger(ResponseParameter.class);
	
	private ResponseHeader header = new ResponseHeader();
	
	private ResponseBody body = new ResponseBody();
	
	private String requestSource;

	public ResponseHeader getHeader() {
		return header;
	}

	public void setHeader(ResponseHeader header) {
		this.header = header;
	}

	public ResponseBody getBody() {
		return body;
	}

	public void setBody(ResponseBody body) {
		this.body = body;
	}
	
	public String getRequestSource() {
		return requestSource;
	}

	public void setRequestSource(String requestSource) {
		this.requestSource = requestSource;
	}

	public ResponseParameter(){}
	
	/**
	 * 带参构造方法
	 * @param requestType 请求类型
	 * @param userId 请求用户
	 * @param appVersion 应用版本
	 * @param macNo 设备唯一标示
	 */
	public ResponseParameter(String requestType,String userId,String appVersion,String macNo){
		header = new ResponseHeader();
		header.setRequestType(requestType);
		header.setLoginUserId(userId);
		header.setAppVersionCode(appVersion);
		header.setMacNo(macNo);
	}
	
	/**
	 * 通过请求head 填充响应头信息
	 * @param requestType 请求类型
	 * @param requestHeader
	 */
	public ResponseParameter(String requestType,RequestHeader requestHeader,String requestSource){
		header = new ResponseHeader();
		header.setRequestType(requestType);
		header.setLoginUserId(requestHeader.getLoginUserId());
		header.setAppVersionCode(requestHeader.getAppVersionCode());
		header.setMacNo(requestHeader.getMacNo());
		this.requestSource = requestSource;
	}
	
	public ResponseParameter addData(Object data){
		if(this.getBody()==null){
			this.body = new ResponseBody();
		}
		this.body.setData(data);
		return this;
	}
	
	public ResponseParameter addPage(Page page){
		if(this.getBody()==null){
			this.body = new ResponseBody();
		}
		this.body.setPage(page);
		return this;
	}
	
	public ResponseParameter addError(String errorCode){
		return this.addError(errorCode, null);
	}
	
	public ResponseParameter addError(String errorCode,String errorDesc){
		if(this.getBody()==null){
			this.body = new ResponseBody();
		}
		if(StringUtils.isEmpty(errorCode)){
			errorCode = Constant.ReturnCode.CODE_0000;
		}
		if(StringUtils.isEmpty(errorDesc)){
			errorDesc = StringUtils.isEmpty(Constant.ReturnCodeInfoMap.get(errorCode)) ?  Constant.ReturnCodeInfoMap.get(Constant.ReturnCode.CODE_0000) : Constant.ReturnCodeInfoMap.get(errorCode);
		}
		this.body.setResultCode(errorCode);
		this.body.setResultDesc(errorDesc);
		return this;
	}
	
	public ResponseParameter setResultCode(String returnCode){
		return this.setResultCode(returnCode, null);
	}
	
	public ResponseParameter setResultCode(String returnCode,String desc){
		if(this.getBody()==null){
			this.body = new ResponseBody();
		}
		if(StringUtils.isEmpty(returnCode)){
			returnCode = Constant.ReturnCode.CODE_1000;
		}
		if(StringUtils.isEmpty(desc)){
			desc = StringUtils.isEmpty(Constant.ReturnCodeInfoMap.get(returnCode)) ?  Constant.ReturnCode.CODE_1000 : Constant.ReturnCodeInfoMap.get(returnCode);
		}
		this.body.setResultCode(returnCode);
		this.body.setResultDesc(desc);
		return this;
	}
	
	public String toJson() {
		String jsonStr = JsonUtils.toJson(this);
		logger.info("加密前参数:【出参：{}】", jsonStr);
		//h5的加密方式 3 ajax请求 4-angular请求 主要是返回数据格式不一样
		try {
			if ("3".equals(requestSource)) {
				logger.info("*****ajax跨域*****H5的加密方式*********");
				Des3H5 des = new Des3H5();
				// DES加密
				String str = des.encrypt(jsonStr);
				H5Content h5 = new H5Content();
				h5.setRetValue(str);
				jsonStr = "jsonpCallback" + "(" + JsonUtils.toJson(h5) + ")";
//				jsonStr = "jsonpCallback" + "(" + str + ")";
				return jsonStr;
			}else if("4".equals(requestSource)){
				logger.info("******angular跨域****H5的加密方式*********");
				Des3H5 des = new Des3H5();
				// DES加密
				String str = des.encrypt(jsonStr);
				H5Content h5 = new H5Content();
				h5.setRetValue(str);
				jsonStr = JsonUtils.toJson(h5);
				return jsonStr;
				
			}else{
				logger.info("**********非H5的加密方式*********");
				jsonStr = Des3Utils.encode(jsonStr);
			}	
			return jsonStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public String toWxSmallJson() {
		String jsonStr = JsonUtils.toJson(this);
		return jsonStr;
	}
	public String toJson1(){
		String jsonStr = JsonUtils.toJson(this);
		logger.info("【出参：{}】", jsonStr);
		return jsonStr;
	}
	
	public ResponseParameter setDataVersion(String dataVersion){
		if(this.getBody()==null){
			this.body = new ResponseBody();
		}
		this.body.setDataVersion(dataVersion);
		return this;
	}
	
}
